package com.itheima.controller;

import com.itheima.pojo.Emp;
import com.itheima.pojo.PageBean;
import com.itheima.pojo.Result;
import com.itheima.service.EmpService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.List;

/**
 * 员工管理的controller
 */
@Slf4j
@RestController
@RequestMapping("/emps")
public class EmpController {

    //通过Autowired注入service层的接口,面向接口开发
    @Autowired
    private EmpService empService;

    /*
    //普通写法
    @GetMapping("/emps")
    public Result page(Integer page,Integer pageSize){
        if(page == null) page = 1;
        if(pageSize == null) pageSize = 10;
        log.info("分页查询,参数:(),()",page,pageSize);
    }
     */

    //高级写法

    @GetMapping
    public Result page(@RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "10") Integer pageSize,
                       String name, Short gender,
                       @DateTimeFormat(pattern = "yyyy-mm-dd") LocalDate begin,
                       @DateTimeFormat(pattern = "yyyy-mm-dd") LocalDate end

                       )
    {
        log.info("分页查询,参数:{},{},{},{},{},{}",page,pageSize,name,gender,begin,end);

        //调用service中的方法进行分页查询
        PageBean pageBean = empService.page(page,pageSize,name,gender,begin,end);
        //把pageBean封装到Result对象当中返回
        return Result.success(pageBean);
    }


    @DeleteMapping("/{ids}")
    public Result delete(@PathVariable List<Integer> ids){
        log.info("批量删除操作,ids:{}",ids);
        empService.delete(ids);
        return Result.success();
    }

    @PostMapping
    public Result save(@RequestBody Emp emp){
        log.info("新增员工,emp:{}",emp);
        empService.save(emp);
        return Result.success();

    }


    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id){
        log.info("根据id查询员工信息,id:{}",id);
        Emp emp = empService.getById(id);
        return Result.success(emp);
    }

    @PutMapping
    public Result update(@RequestBody Emp emp){
        log.info("更新员工信息:{}",emp);
        empService.update(emp);
        return Result.success();

    }

}
